package com.example.abe.diandi.home.fragment;

import android.content.Intent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.BindView;
import com.example.OnClick;
import com.example.abe.diandi.R;
import com.example.abe.diandi.base.BaseFragment;
import com.example.abe.diandi.category.activity.CategoryActivity;
import com.example.abe.diandi.category.model.CategoryListModel;
import com.example.abe.diandi.collect.activity.CollectActivity;
import com.example.abe.diandi.main.AppConfig;
import com.example.abe.diandi.nohttp.ApiUtil;
import com.example.abe.diandi.nohttp.CallServer;
import com.example.abe.diandi.nohttp.HttpListener;
import com.example.abe.diandi.user.activity.LoginActivity;
import com.example.abe.diandi.user.activity.UserInfoActivity;
import com.example.abe.diandi.util.ImageUtil;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.yanzhenjie.nohttp.NoHttp;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.Request;
import com.yanzhenjie.nohttp.rest.Response;

import example.abe.com.framework.imageloader.ImageLoader;
import example.abe.com.framework.util.ToastUtil;

import static com.example.abe.diandi.R.id.frag_user_drawer_iv_icon;
import static com.example.abe.diandi.nohttp.ApiUtil.METHOD;
import static com.example.abe.diandi.nohttp.ApiUtil.M_USER_GET_CATEGORY_LIST;
import static com.example.abe.diandi.nohttp.ApiUtil.SERVICE;
import static com.example.abe.diandi.nohttp.ApiUtil.S_USER;

/**
 * Created by abe on 17/1/12.
 */

public class MainDrawerFragment extends BaseFragment {

    @BindView(frag_user_drawer_iv_icon)
    protected ImageView mIvIcon;

    @BindView(R.id.frag_user_drawer_tv_user_name)
    protected TextView mTvName;

    @BindView(R.id.frag_user_drawer_tv_diandi_count)
    protected TextView mTvDiandiCount;

    @BindView(R.id.frag_user_drawer_ll_root)
    protected LinearLayout mLlRoot;

    public static MainDrawerFragment instance() {
        return new MainDrawerFragment();
    }

    @Override
    public int getLayoutID() {
        return R.layout.fragment_user_drawer;
    }

    @Override
    public void initData() {
    }

    @Override
    public void initView() {
        ImageLoader.getInstance().getImage(mIvIcon, AppConfig.getUserModel().getIcon());
        mTvName.setText(AppConfig.getUserModel().getName());
        mTvDiandiCount.setText(AppConfig.getCategoryList().get(0).getCount() + "个点滴");

        //防止点击事件穿透
        mLlRoot.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
    }

    @Override
    public void onResume(){
        super.onResume();
        updateCategoryList();
    }

    /**
     * 更新类别列表
     */
    private void updateCategoryList() {
        Request<String> request = NoHttp.createStringRequest(ApiUtil.URL, RequestMethod.POST);
        request.add(SERVICE, S_USER)
                .add(METHOD, M_USER_GET_CATEGORY_LIST)
                .add("userId", AppConfig.getUserModel().getId());
        CallServer.getInstance().add(getActivity(), false, request, new HttpListener<String>() {

            @Override
            public void onSucceed(int what, Response<String> response) {
                JsonObject jsonObj = new JsonParser().parse(response.get()).getAsJsonObject();
                Boolean isSuccess = jsonObj.get("success").getAsBoolean();
                if (isSuccess) {
                    JsonObject data = jsonObj.getAsJsonObject("data");
                    CategoryListModel model = new Gson().fromJson(data, CategoryListModel.class);
                    AppConfig.setCategoryList(model.getList());
                    mTvDiandiCount.setText(AppConfig.getCategoryList().get(0).getCount() + "个点滴");
                }
            }

            @Override
            public void onFailed(int what, Response<String> response) {
            }
        });
    }

    @OnClick({R.id.frag_user_drawer_ll_category, R.id.frag_user_drawer_ll_collect, R.id.frag_user_drawer_ll_setting, R.id.frag_user_drawer_ll_share, frag_user_drawer_iv_icon})
    protected void onToNextActivityClick(View view) {
        switch (view.getId()) {
            case R.id.frag_user_drawer_ll_category:
                Intent intent = new Intent(getActivity(), CategoryActivity.class);
                startActivity(intent);
                break;
            case R.id.frag_user_drawer_ll_collect:
                intent = new Intent(getActivity(), CollectActivity.class);
                startActivity(intent);
                break;
            case R.id.frag_user_drawer_iv_icon:
                intent = new Intent(getActivity(), UserInfoActivity.class);
                startActivity(intent);
                break;
            case R.id.frag_user_drawer_ll_setting:
                ToastUtil.makeText("设置");
                break;
            case R.id.frag_user_drawer_ll_share:
                ToastUtil.makeText("分享");
                break;
        }
    }

    @OnClick({R.id.frag_user_drawer_tv_sign_out})
    protected void onSingOutClick() {
        Intent intent = new Intent(getActivity(), LoginActivity.class);
        startActivity(intent);
        getActivity().finish();
        AppConfig.clear();
    }
}

